export default class Set {
  constructor() {
    this.obj = {};
  }
  add(value) {
    if (this.has(value)) {
      return false;
    }
    this.obj[value] = value;
    return true;
  }
  remove(value) {
    if (!this.has(value)) {
      return false;
    }
    delete this.obj[value];
    return true;
  }
  has(value) {
    return this.obj.hasOwnProperty(value);
  }
  clear() {
    this.obj = {};
  }
  size() {
    return Object.keys(this.obj).length;
  }
  values() {
    return Object.keys(this.obj);
  }
  // 并集
  union(otherSet) {
    let unionSet = new Set(),
      values = this.values();
    for (let i = 0; i < values.length; i++) {
      unionSet.add(values[i]);
    }
    values = otherSet.values();
    for (let j = 0; j < values.length; j++) {
      unionSet.add(values[j]);
    }
    return unionSet;
  }
  // 交集
  intersection(otherSet) {
    let intersectionSet = new Set(),
      values = this.values();
    for (let i = 0; i < values.length; i++) {
      if (otherSet.has(values[i])) {
        intersectionSet.add(values[i]);
      }
    }
    return intersectionSet;
  }
  // 差集
  difference(otherSet) {
    let differenceSet = new Set(),
      values = this.values();
    for (let i = 0; i < values.length; i++) {
      if (!otherSet.has(values[i])) {
        differenceSet.add(values[i]);
      }
    }
    return differenceSet;
  }
  // 子集
  subSet(otherSet) {
    let values = this.values();
    for (let i = 0; i < values.length; i++) {
      if (!otherSet.has(values[i])) {
        return false;
      }
    }
    return true;
  }
}